import {
  LockOutlined,
  UserOutlined,
} from '@ant-design/icons';
import {
  LoginForm,
  ProFormText,
} from '@ant-design/pro-components';
import { useEmotionCss } from '@ant-design/use-emotion-css';
import { message, Typography, Modal } from 'antd';
import React from 'react';
import Settings from '../../../../config/defaultSettings';
import * as authController from '@/services/backend/authController';

const { Link } = Typography;

const Login: React.FC = () => {
  const containerClassName = useEmotionCss(() => {
    return {
      display: 'flex',
      flexDirection: 'column',
      height: '100vh',
      overflow: 'auto',
      backgroundImage:
        "url('https://mdn.alipayobjects.com/yuyan_qk0oxh/afts/img/V-_oS6r-i7wAAAAAAAAAAAAAFl94AQBr')",
      backgroundSize: '100% 100%',
    };
  });

  const handleSubmit = async (values: any) => {
    try {
      const result = await authController.login(values);
      if (result.code === 1) {
        const userInfo = {
          userId: result.data.userId,
          userName: result.data.userName,
          userRole: result.data.isAdmin ? 'admin' : 'user',
          isAdmin: result.data.isAdmin,
          token: result.data.token,
        };
        localStorage.setItem('currentUser', JSON.stringify(userInfo));
        localStorage.setItem('token', result.data.token || '');
        message.success('登录成功！');
        // 根据用户角色跳转到不同页面
        window.location.href = userInfo.isAdmin ? '/customer-manage' : '/cards';
        return;
      } else {
        message.error(result.msg || '登录失败，请检查用户名和密码！');
      }
    } catch (error) {
      console.error('登录异常:', error);
      message.error('登录失败，请稍后重试！');
    }
  };

  const handleSurpriseClick = (e: React.MouseEvent) => {
    e.preventDefault();
    Modal.success({
      title: '天天开心',
      content: '祝你天天开心，永远快乐！❤️',
      okText: '谢谢',
    });
  };

  return (
    <div className={containerClassName}>
      <div
        style={{
          flex: '1',
          padding: '32px 0',
        }}
      >
        <LoginForm
          contentStyle={{
            minWidth: 280,
            maxWidth: '75vw',
          }}
          logo={<img alt="logo" src="/logo.svg" />}
          title="大富翁银行"
          subTitle="欢迎使用大富翁银行系统"
          onFinish={handleSubmit}
        >
          <ProFormText
            name="loginId"
            fieldProps={{
              size: 'large',
              prefix: <UserOutlined />,
            }}
            placeholder="用户名/手机号"
            rules={[
              {
                required: true,
                message: '请输入用户名/手机号!',
              },
            ]}
          />
          <ProFormText.Password
            name="password"
            fieldProps={{
              size: 'large',
              prefix: <LockOutlined />,
            }}
            placeholder="密码"
            rules={[
              {
                required: true,
                message: '请输入密码！',
              },
            ]}
          />
        </LoginForm>
      </div>
      <div style={{ textAlign: 'center', padding: '24px', background: 'rgba(255,255,255,0.8)' }}>
        <a href="https://gitee.com/hello77ismikey/bank-fronted.git" target="_blank" style={{ color: '#333', textDecoration: 'none' }}>前端仓库</a>
        <span style={{ margin: '0 8px' }}>|</span>
        <a href="https://gitee.com/hello77ismikey/bank-backend.git" target="_blank" style={{ color: '#333', textDecoration: 'none' }}>后端仓库</a>
        <span style={{ margin: '0 8px' }}>|</span>
        <a href="#" onClick={handleSurpriseClick} style={{ color: '#333', textDecoration: 'none' }}>Surprise!!!!</a>
        <div style={{ marginTop: '8px', color: '#333' }}>
          2025 Create By 琪琪&晶晶
        </div>
      </div>
    </div>
  );
};

export default Login;

